package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author fan
 * @since 2021-08-21
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        // 允许那些域名跨域访问(origin,浏览器地址)：不要写*，允许所有域名跨域访问，无法携带cookie
        corsConfiguration.addAllowedOrigin("http://manager.gmall.com");
        //此ip也能访问商城页面
        corsConfiguration.addAllowedOrigin("http://localhost:1000");
        // 是否允许携带cookie
        corsConfiguration.setAllowCredentials(true);
        // 允许所有请求方式跨域访问
        corsConfiguration.addAllowedMethod("*");
        // 允许携带任何头信息访问
        corsConfiguration.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(configurationSource);
    }

}
